<?php
/*
 * Created on 2013-9-14
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
function auto_charset($fContents, $from='gbk', $to='utf-8'){
	$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
	$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
	if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)))
	{
		//如果编码相同或者非字符串标量则不转换
		return $fContents;
	}
	if (is_string($fContents))
	{
		if (function_exists('mb_convert_encoding'))
		{
			return mb_convert_encoding($fContents, $to, $from);
		}elseif (function_exists('iconv')) {
			return iconv($from, $to, $fContents);
		}else {
			return $fContents;
		}
	}elseif (is_array($fContents)) {
		foreach ($fContents as $key => $val){
			$_key = auto_charset($key, $from, $to);
			$fContents[$_key] = auto_charset($val, $from, $to);
			if ($key != $_key) unset($fContents[$key]);
		}
		return $fContents;
	}else {
		return $fContents;
	}
}





 function decrypt ($decrypt){
	$key = C('DES_KEY');

    // 不需要設定 IV
    $str = mcrypt_decrypt(MCRYPT_DES, $key, base64_decode($decrypt), MCRYPT_MODE_ECB);

    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 移除 Padding
    $pad = ord($str[strlen($str) - 1]);
    return substr($str, 0, strlen($str) - $pad);
}

//加密
function encrypt ($encrypt){
	$key = C('DES_KEY');

    // 根據 PKCS#7 RFC 5652 Cryptographic Message Syntax (CMS) 修正 Message 加入 Padding
    $block = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_ECB);
    $pad = $block - (strlen($encrypt) % $block);
    $encrypt .= str_repeat(chr($pad), $pad);

    // 不需要設定 IV 進行加密
    $passcrypt = mcrypt_encrypt(MCRYPT_DES, $key, $encrypt, MCRYPT_MODE_ECB);
    return base64_encode($passcrypt);
}



function add_log($adminid,$content,$type){
	$m = M('admin_log');
	$data['admin_id'] = $adminid;
	$data['action_content'] = $content;
	$data['addtime'] = time();
	$data['type'] = $type;
	$data['fromip'] = $_SERVER['REMOTE_ADDR'];
	$m->add($data);
}

function getadminname($id,$type){
	if($type == 1){
		$m = M('admin');
		$list= $m->where('id='.$id)->find();
		return $list['username'];
	}else if($type ==2){
		$m = M('teacher');
		$list= $m->where('id='.$id)->find();
		return $list['XM'];
	}

}


function getis_teacher($status){
	if($status ==1 ){
		return "老师";
	}else if($status == 0){
		return "";
	}
}

function getis_school($status){
	if($status ==1 ){
		return "学校管理员";
	}else if($status == 0){
		return "";
	}
}


function getis_parent($status){
	if($status ==1 ){
		return "家长";
	}else if($status == 0){
		return "";
	}
}

function getstatus($status){
	if($status == 1){
		return "审核通过";
	}else{
		return "未审核";
	}
}

function getschoolname($id){
	$m = M('school');
	$list = $m->find($id);
	return $list['XXMC'];
}


function add1log($adminid,$content,$type,$from=1){
	$m = M('admin_log');
	$data['admin_id'] = $adminid;
	$data['action_content'] = $content;
	$data['addtime'] = time();
	$data['type'] = $type;
	$data['fromip'] = $_SERVER['REMOTE_ADDR'];
	$data['from'] = $from;
	$logid = $m->add($data);
	return $logid;
}


function outlog($logid){
	$m = M('admin_log');
	$data['id'] = $logid;
	$data['logouttime'] = time();
	$m->save($data);
}


function logintime($login,$logout){
	$time = $logout-$login;
	$time = $time/60;
	return ceil($time);
}


function getschoolusername($school){
	$m = M('teacher');
	$map['school_id'] = $school;
	$teacherlist = $m->where($map)->select();

	$arr="";
	foreach($teacherlist as $k=>$l){
		$arr[$k] = $l['user_id'];
	}

	$user = M('user');
	$uu['id'] = array('in',$arr);
	$uu['is_teacher']=1;
	$uu['is_school']=1;
	$userinfo = $user->where($uu)->find();
	return $userinfo['username'];
}


function getusername($userid){
	$user = M('user');
	$list = $user->find($userid);
	return $list['username'];
}


function randchar()
{

    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    for ($i = 0; $i < 8; $i++)
    {
        $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $str;
}

function loadschoolpower($id,$t)
{
	$map['type']=$t;
	$mpower =M('power');
	$plist = $mpower->where($map)->select();
	$powerid = getinfo($id,'school','power');

	foreach ($plist as $pl ) {
		if ($mpower->where($pl['id'] .' in ('.$powerid.')')->count()>0)
		{
			$c='checked';
		}
		else
		{
			$c='';
		}
		$str .=  '<div style="width:120px; float:left"><input class="inputcheckbox " name="powerid[]" value="'.$pl['id'].'" type="checkbox" '.$c.'> '.$pl['power_name'].'&nbsp&nbsp&nbsp</div>';
		
	}
	return $str;
}

function getinfo($id,$table,$field)
{
	$parent = M($table);
	if (empty($id))
	{
		return '';
	}
	$info = $parent ->find($id);
	return $info[$field];
	unset($parent);
}
?>

